package com.zsix.system.strategy.userByTaskRule;

import com.zsix.system.mapper.orgmanagementMapper.UserEntityMapper;
import com.zsix.system.mapper.taskAssigneeMapper.ApprovalTrackMapper;
import com.zsix.system.service.IAssigneeUserService;
import com.zsix.common.entity.taskAssignee.ApprovalTrack;
import com.zsix.common.entity.taskAssignee.AssigneeUserEntity;
import com.zsix.common.util.RedisUtils;
import com.zsix.common.util.StringUtils;
import com.zsix.common.util.SystemConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * 按照指定节点处理人的部门负责人查找节点处理人
 */
@Component
public class ByTaskManager implements UserByTaskRuleStrategy {
    @Autowired
    private IAssigneeUserService assigneeUserService;
    @Autowired
    private ApprovalTrackMapper approvalTrackMapper;
    @Autowired
    private UserEntityMapper userEntityMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Override
    public HashMap<String,Object> getUserByTaskRule(String taskid, String ruleName
            , HashMap<String,Object> variables, String procInstId) {
        //当前登陆人信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        //查找绑定的接收人
        HashMap<String, Object> hashMap = assigneeUserService
                .getAssigneeUserWithTaskKey(taskid, ruleName);
        List<AssigneeUserEntity> assigneeUserEntityList = (List<AssigneeUserEntity>) hashMap.get("assigneeUserEntityList");
        String nextWorkEmp = "";
        for (AssigneeUserEntity ae : assigneeUserEntityList) {
            String sender="";
            String manager="";
            List<ApprovalTrack> approvalTrackList=approvalTrackMapper.getUserTaskTrack(procInstId);
            if(approvalTrackList.size()>0){
                for (ApprovalTrack approvalTrack:approvalTrackList){
                    if(ae.getUsername().equals(approvalTrack.getUserTaskId())) {
                        sender = approvalTrack.getUserName();
                        break;
                    }
                }
                if(StringUtils.isNull(sender))
                    manager=userEntityMapper.getUserManager(username).getUsername();
                else
                    manager=userEntityMapper.getUserManager(sender).getUsername();
            }
            else{
                manager=userEntityMapper.getUserManager(username).getUsername();
            }
            nextWorkEmp += manager + ",";
        }
        if(!StringUtils.isNull(nextWorkEmp)){
            nextWorkEmp = nextWorkEmp.substring(0, nextWorkEmp.length() - 1);
        }

        //放入缓存，用于发送消息推送
        variables.put(taskid + "_byTaskManager", nextWorkEmp);
        if(redisUtils.exists(procInstId+"_sms")){
            redisUtils.remove(procInstId+"_sms");
        }
        redisUtils.set(procInstId+"_sms",nextWorkEmp);
        return variables;
    }
    @Override
    public String getType(){
        return "byTaskManager";
    }
}
